package com.zs.hydrant.controller;

import com.zs.hydrant.entity.Feedback;
import com.zs.hydrant.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/feedback")
public class FeedbackController {
    @Autowired
    private FeedbackService feedbackService;

    // 提交反馈（需用户登录，携带token）
    @PostMapping("/submit")
    public Map<String, Object> submitFeedback(@RequestBody Feedback feedback) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 实际场景需从token中获取userId，这里简化为前端传参
            if (feedbackService.submitFeedback(feedback)) {
                result.put("success", true);
                result.put("msg", "反馈提交成功");
                result.put("code", 200);
            } else {
                result.put("success", false);
                result.put("msg", "反馈提交失败");
                result.put("code", 500);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", "提交失败：" + e.getMessage());
            result.put("code", 500);
            e.printStackTrace();
        }
        return result;
    }

    // 1. 获取待处理反馈列表
    @GetMapping("/list")
    public Map<String, Object> getPendingFeedback() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Feedback> feedbackList = feedbackService.getByStatus(0);
            result.put("success", true);
            result.put("msg", "获取成功");
            result.put("data", feedbackList);
            result.put("total", feedbackList != null ? feedbackList.size() : 0);
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", "获取失败：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    // 2. 标记反馈为已处理
    @PostMapping("/handle")
    public Map<String, Object> handleFeedback(@RequestBody Map<String, Integer> params) {
        Map<String, Object> result = new HashMap<>();
        try {
            Integer feedbackId = params.get("id");
            if (feedbackId != null) {
                feedbackService.updateStatus(feedbackId, 1); // 将status改为1（已处理）
                result.put("success", true);
                result.put("msg", "处理成功");
                result.put("code", 200);
            } else {
                result.put("success", false);
                result.put("msg", "无效的反馈ID");
                result.put("code", 400);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", "处理失败：" + e.getMessage());
            result.put("code", 500);
            e.printStackTrace();
        }
        return result;
    }

}